package myself.a011HowAreYou;

import java.util.PriorityQueue;

/**
 * @author 叶之越
 * Description 分黄金问题
 * Date 2021/1/30
 * Time 22:05
 * Mail 739153436@qq.com
 */
public class LessMoneySplitGold {

    // 使用堆来解决，前人的经验
    public static int lessMoney2(int[] arr) {
        if (arr == null || arr.length == 0) {
            return 0;
        }
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        for (int i : arr) {
            heap.add(i);
        }
        int result = 0;
        while (heap.size() > 1) {
            int split = heap.poll() + heap.poll();
            heap.add(split);
            result += split;
        }
        return result;
    }
}
